package com.adou.shiro.token;

import com.adou.common.model.User;
import com.adou.common.util.MD5Util;
import com.adou.common.util.MemSession;
import com.adou.common.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * @author crg
 */
public class TokenManager {
    //用户登录管理

    /**
     * 获取当前登录的用户User对象
     *
     * @return
     */
    public static User getToken() {
        User token = (User) SecurityUtils.getSubject().getPrincipal();
        return token;
    }

    /**
     * 获取当前用户的Session
     *
     * @return
     */
    public static Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }

    /**
     * 获取当前用户登陆ID
     *
     * @return
     */
    public static String getUserId() {
        return getToken() == null ? null : getToken().getCuserid();
    }

    /**
     * 获取当前用户ID
     *
     * @return
     */
    public static Long getId() {
        return getToken() == null ? null : getToken().getId();
    }

    /**
     * 根据KEY从会话获取当前用户ID
     *
     * @return
     */
    public static Long getIdByKey() {
        Long id = (Long) getSession().getAttribute("id");
        return id;
    }

    /**
     * 根据KEY从会话获取当前登陆用户
     *
     * @return
     */
    public static String getUserIdByKey() {
        String userid = (String) getSession().getAttribute("userid");
        return userid;
    }

    /**
     * 根据KEY从会话获取当前登陆用户名称
     *
     * @return
     */
    public static String getUserNameByKey() {
        String username = (String) getSession().getAttribute("username");
        return username;
    }

    /**
     * 根据KEY从会话获取当前系统类型
     *
     * @return
     */
    public static String getSysTypeByKey() {
        String systype = (String) getSession().getAttribute("systype");
        return systype;
    }

    /**
     * 根据KEY从会话获取登陆会话ID
     *
     * @return
     */
    public static String getSessionIdByKey() {
        String sessionid = (String) getSession().getAttribute("sessionid");
        return sessionid;
    }

    /**
     * 把值放入到当前登录用户的Session里
     *
     * @param key
     * @param value
     */
    public static void setVal2Session(Object key, Object value) {
        getSession().setAttribute(key, value);
    }

    /**
     * 从当前登录用户的Session里取值
     *
     * @param key
     * @return
     */
    public static Object getVal2Session(Object key) {
        return getSession().getAttribute(key);
    }

    /**
     * 登录
     *
     * @param user
     * @param rememberMe
     * @return
     */
    public static User login(User user, Boolean rememberMe) {
        ShiroToken token = new ShiroToken(user.getCuserid(), user.getCpswd());
        token.setRememberMe(rememberMe);
        SecurityUtils.getSubject().login(token);
        return getToken();
    }

    /**
     * 判断是否登录
     *
     * @return
     */
    public static boolean isLogin() {
        return null != SecurityUtils.getSubject().getPrincipal();
    }

    /**
     * 退出登录
     */
    public static void logout() {
        SecurityUtils.getSubject().logout();
    }

    public static String getMemcacheBySessionid(String userid, String ip) {
        String sessionid = null;
        try {
            MemSession cache = MemSession.getInstance();
            String md5 = MD5Util.GetMD5Code(StringUtil.replaceBeginAndEnd(userid + ip));
            Object obj = cache.get(md5);
            if (obj != null) {
                sessionid = obj.toString();
                cache.replace(md5, sessionid, new Date(12 * 60 * 60 * 1000));
            }
        } catch (Exception ex) {
        }
        return sessionid;
    }

    public static void setMemcacheBySessionid(String userid, String ip, String sessionid) {
        try {
            MemSession cache = MemSession.getInstance();
            String md5 = MD5Util.GetMD5Code(StringUtil.replaceBeginAndEnd(userid + ip));
            Object obj = cache.get(md5);
            if (obj != null) {
                cache.replace(md5, sessionid, new Date(12 * 60 * 60 * 1000));
            }
        } catch (Exception ex) {
        }
    }

    public static JSONObject getMemcacheByUser(String sessionid) {
        JSONObject userinfo = new JSONObject();
        try {
            MemSession cache = MemSession.getInstance();
            userinfo = JSONObject.parseObject(cache.get("JSON-" + sessionid).toString());
            if (userinfo != null && userinfo.size() > 0) {
                cache.replace("JSON-" + sessionid, userinfo.toString(), new Date(12 * 60 * 60 * 1000));
            }
        } catch (Exception ex) {
        }
        return userinfo;
    }

    public static void setMemcacheByUser(String sessionid, JSONObject json) {
        try {
            MemSession cache = MemSession.getInstance();
            Object obj = cache.get("JSON-" + sessionid);
            if (obj != null) {
                cache.replace("JSON-" + sessionid, json.toString(), new Date(12 * 60 * 60 * 1000));
            }
        } catch (Exception ex) {
        }
    }
}
